// 云函数入口文件
const cloud = require('wx-server-sdk')
const Core = require('@alicloud/pop-core');

cloud.init()

// 云函数入口函数
exports.main = async (event, context) => {
  const wxContext = cloud.getWXContext()

  var client = new Core({
    accessKeyId: event.accessKeyId,
    accessKeySecret: event.accessKeySecret,
    endpoint: 'https://dysmsapi.aliyuncs.com',
    apiVersion: '2017-05-25'
  });

  var params = {
    "PhoneNumbers": event.PhoneNumbers,
    "SignName": event.SignName,
    "TemplateCode": event.TemplateCode,
    "TemplateParam": event.TemplateParam
  }

  var requestOption = {
    method: 'POST'
  };
  var res;
  await client.request('SendSms', params, requestOption).then((result) => {
    console.log(result);
    res = result;
  }, (ex) => {
    console.log(ex);
    res = ex;
  })
  
  return {
    event,
    openid: wxContext.OPENID,
    appid: wxContext.APPID,
    unionid: wxContext.UNIONID,
    result: res
  }
}